package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"travel/api/client"
	"travel/api/request"
	"travel/rpc/proto/travel"
)

func Register(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到此参数",
			"data": nil,
		})
		return
	}
	register, err := client.Register(c, &travel.RegisterReq{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"coed": 200,
		"msg":  "注册成功",
		"data": register,
	})
}

func Login(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "参数获取失败",
			"data": nil,
		})
		return
	}
	login, err := client.Login(c, &travel.LoginReq{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "登录失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": login,
	})
}

func GoodsCreate(c *gin.Context) {
	var data request.GoodsCreate
	err := c.ShouldBind(&data)
	fmt.Println(err)
	create, err := client.GoodsCreate(c, &travel.GoodsCreateReq{
		Name:  data.Name,
		Num:   data.Num,
		Dates: data.Dates,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品库存不足",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品库存扣减成功",
		"data": create,
	})
}

func RealName(c *gin.Context) {
	var data request.RealName
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	name, err := client.RealName(c, &travel.RealNameReq{
		UserId:  data.UserId,
		ReaInes: data.ReaInes,
		IdCard:  data.IdCard,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "实名失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "实名成功",
		"data": name,
	})
}

func OrderSn(c *gin.Context) {
	var data request.OrderSn
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	sn, err := client.OrderSn(c, &travel.OrderSnReq{
		UserId:  data.UserId,
		GoodsId: data.GoodsId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "下单失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "下单成功",
		"data": sn,
	})
}

func Insure(c *gin.Context) {
	var data request.OrderUser
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	insure, err := client.Insure(c, &travel.InsureReq{
		Userid: data.UserId,
		Name:   data.Name,
		Price:  float32(data.Price),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "投保失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "投保成功",
		"data": insure,
	})
}

func Callback(c *gin.Context) {
	var data request.CallBack
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	callback, err := client.Callback(c, &travel.CallbackReq{
		Status: int64(data.Status),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "回调修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "回调修改成功",
		"data": callback,
	})
}

func ScenicSo(c *gin.Context) {
	var data request.ScenicSo
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	so, err := client.ScenicSo(c, &travel.ScenicSoReq{
		Name: data.Name,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "搜索失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "搜索成功",
		"data": so,
	})
}

func OrderFind(c *gin.Context) {
	var data request.OrderFind
	err := c.ShouldBind(&data)
	so, err := client.OrderFind(c, &travel.OrderFindReq{OrderId: int64(data.OrderId)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": so,
	})
}

func Refund(c *gin.Context) {
	var data request.Refund
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "未接收到参数",
			"data": nil,
		})
		return
	}
	so, err := client.Refund(c, &travel.RefundReq{
		Status:  int64(data.Status),
		Goodsid: data.GoodsId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "退单失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "退单成功",
		"data": so,
	})
}
